package com.hcc.security.service.serviceImpl;

import com.hcc.security.enetity.Role;
import com.hcc.security.enetity.User;
import com.hcc.security.mapper.RoleMapper;
import com.hcc.security.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @Author: HC
 * @Date: 2019/4/23 16:41
 * @Version 1.0
 */
@Service
public class SecurityServiceImpl implements UserDetailsService {

  @Autowired
  private UserMapper userMapper;
  @Autowired
  private RoleMapper roleMapper;

  @Override
  public UserDetails loadUserByUsername(String loginName) throws UsernameNotFoundException {
    //返回 user
    User user = new User();
    user.setLoginName(loginName);
    user = userMapper.findByLoginName(loginName);
    if(user == null){
      throw new UsernameNotFoundException("用户名或密码不正确");
    }
    user.setRoles(roleMapper.selectRolesByUserId(user.getId()));
    return user;
  }
}
